import sys
import threading
import time
import os
from getip import getip

uri = None
uri2 = None
heads = int(sys.argv[1])

pids = set()
for n in xrange(0,heads):
    time.sleep(1)
    pid = os.fork()
    if pid == 0:
        port = 4000+n
        uri = getip()+":"+str(port-1)
        uri2 = getip()+":"+str(port-2)
        if n == 0:
            argv = ["fserv",str(port),"--walk"]
        elif n == 1:
            argv = ["fserv",str(port),uri]
        else:
            argv = ["fserv",str(port),uri,uri2]
        import fserv
        print argv
        fserv.main(argv)
    else:
        pids.add(pid)

print 'setup complete'

while len(pids)>0:
    try:
        (pid,status)=os.wait()
        if pid in pids:
            print 'pid',pid
            pids.remove(pid)
    except KeyboardInterrupt:
        pass
